package com.xq.shop.utils;

import com.xq.shop.vo.GoodsTypeVo;
import com.xq.shop.vo.MenuVo;
import utils.BaseConstants;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author gaoxiaojin
 */
public class MyConstants {

    public static String KEY1 = "2024";
    public static String KEY2 = "ywxq";
    public static String KEY3 = "shop";

    public static Map<String, Integer> generateDateRangeMap(Date startDate, Date endDate) {
        Map<String, Integer> dateRangeMap = new LinkedHashMap<>();
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        while (!start.after(end)) {
            String date = BaseConstants.SDF1.format(start.getTime());
            dateRangeMap.put(date, 0);
            start.add(Calendar.DATE, 1);
        }
        return dateRangeMap;
    }

    public static List<Integer> getIdList(String ids) {
        String[] strings = ids.split(",");
        List<String> stringList = Arrays.asList(strings);
        List<Integer> idList = new ArrayList<>();
        stringList.forEach(s -> idList.add(Integer.parseInt(s)));
        return idList;
    }

    public static Date getTodayTime() {
        Calendar calendar = Calendar.getInstance();
        // 设置时间为当天的凌晨（00:00:00）
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取今天凌晨的日期
        return calendar.getTime();
    }

    public static Date getDayTime(Integer value) {
        Calendar calendar = Calendar.getInstance();
        // 设置时间为当天的凌晨（00:00:00）
        calendar.add(Calendar.DAY_OF_MONTH, -value);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取今天凌晨的日期
        return calendar.getTime();
    }

    public static Date getThisMonthTime() {
        Calendar calendar = Calendar.getInstance();
        // 设置为当前月的第一天凌晨
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getLastMonthTime() {
        // 获取当前日期和时间
        Calendar calendar = Calendar.getInstance();
        // 设置为上个月
        calendar.add(Calendar.MONTH, -1);
        // 设置为上个月的第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static List<MenuVo> buildMenuTree(List<MenuVo> menuVoList) {
        List<MenuVo> menuTree = new ArrayList<>();
        for (MenuVo menuVo : menuVoList) {
            if (menuVo.getParentId() == 0) {
                menuTree.add(menuVo);
            }
            for (MenuVo it : menuVoList) {
                if (menuVo.getId().equals(it.getParentId())) {
                    if (menuVo.getChildren() == null) {
                        menuVo.setChildren(new ArrayList<>());
                    }
                    menuVo.getChildren().add(it);
                }
            }
        }
        return menuTree;
    }

    public static List<GoodsTypeVo> buildGoodsTypeTree(List<GoodsTypeVo> goodsTypeVoList) {
        List<GoodsTypeVo> goodsTypeTree = new ArrayList<>();
        for (GoodsTypeVo goodsTypeVo : goodsTypeVoList) {
            if (goodsTypeVo.getFatherId() == 0) {
                goodsTypeTree.add(goodsTypeVo);
            }
            for (GoodsTypeVo it : goodsTypeVoList) {
                if (goodsTypeVo.getId().equals(it.getFatherId())) {
                    if (goodsTypeVo.getChildren() == null) {
                        goodsTypeVo.setChildren(new ArrayList<>());
                    }
                    goodsTypeVo.getChildren().add(it);
                }
            }
        }
        return goodsTypeTree;
    }

    public static List<String> getDateList(Date start, Date end) {
        //结果集
        List<String> dateList = new ArrayList<>();
        //格式化格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/M/d");
        try {
            //计算两日期间相差天数
            long time = (end.getTime() - start.getTime()) / 1000 / 3600 / 24;

            for (int i = 0; i <= time; i++) {
                //创建calendar实例
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(start);
                calendar.add(Calendar.DAY_OF_MONTH, i);
                String value = simpleDateFormat.format(calendar.getTime());
                dateList.add(value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dateList;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate
     *            较小的时间
     * @param bdate
     *            较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);

            return Integer.parseInt(String.valueOf(between_days));
        }catch (Exception e) {
            return 0;
        }
    }
}
